<cfscript>    
    view = createObject("component", session.rootDir & ".views.layout");
    if( Session.Auth EQ True and (Session.strAuthType EQ Session.MANAGERS or Session.strAuthType EQ Session.IT or Session.strAuthType EQ Session.HR) ){
        view.init("Scheduled Archived Users OU Termination", "", "","Home",main, "Termination");
    }else{
        view.init("Denied","","","Home","Denied");
    }    
    view.html();
</cfscript>

<cffunction name="main" access="public" output="true" returntype="any">
    <cfscript>
        import models.*;
        KronosQueries = CreateObject("component", session.rootDir & ".DatabaseQueries.Kronos");        
        ArchiveOUScheduledTerm = ORMExecuteQuery("FROM ArchivedUserOUTermSchedule WHERE DeleteDate <= '#DateFormat( Now(), "MM/DD/YYYY" )#' AND Completed = 0");
    </cfscript>
    
    <h1 class="title">Scheduled Archived Users Folder Termination</h1>
    <div class="entry">
        Scheduled task for deleting the employee's network account after 30 days.<br /><br />
    </div>
    
    <cfset Summary = 'OU Scheduled Archive.  <BR/>Started:  #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR /><hr /><br />'  >
    
    <cftry>
        <cfloop from="1" to="#ArrayLen(ArchiveOUScheduledTerm)#" index="i" >            
            <cfset TermRequest = ORMExecuteQuery("FROM TerminationRequests WHERE TerminationRequestsID = #ArchiveOUScheduledTerm[i].getTermReqID()#") />
            <cfset Employee = KronosQueries.funcGetAllEmployees( TermRequest[1].getRosettaID() )/>
            <cfset Summary &= 'User:  #Employee.FirstName# #Employee.LastName# (#Employee.UserAccount#)<BR/> Started: #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR/><BR/>'/>
            <cfldap action="DELETE" server="10.100.1.101" DN="#ArchiveOUScheduledTerm[i].getArchivedUserOU()#" username="scf\userlist" password="DefinedMan"/>
            <cfscript>
                ArchiveOUScheduledTerm[i].setCompleted( 1 );
                entitySave(ArchiveOUScheduledTerm[i]);       
            </cfscript>        
        </cfloop>
        
    <cfcatch type="Any" >
        <cfmail to=#Session.ITS_EMAIL# From="EPAR_Scheduled@scf.cc" subject="EPAR Scheduled Termination Error" type="html">
            <cfdump var="#CFCATCH#">
        </cfmail>
    </cfcatch>
    </cftry>

    <cfoutput>#Summary#</cfoutput>
    <cfmail to=#Session.ITS_EMAIL# From="EPAR_Scheduled@scf.cc" subject="EPAR Scheduled Termination Summary" type="html">
        #Summary#
    </cfmail>
</cffunction>